package com.pimee.web.sys;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pimee.common.util.StringUtils;
import com.pimee.common.util.bean.BeanUtils;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysDept;
import com.pimee.model.vo.SysDeptVo;
import com.pimee.model.web.Ztree;
import com.pimee.service.admin.ISysDeptService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "admin/sys/dept")
public class SysDeptController extends AbstractController {
	private String prefix = "admin/sys/dept";
	@Autowired
	private ISysDeptService sysDeptService;

	@ApiOperation("加载部门列表树")
	@GetMapping("/treeData")
	@ResponseBody
	public List<Ztree> treeData() {
		List<Ztree> ztrees = sysDeptService.treeDept();
		return ztrees;
	}

	@ApiOperation("选择部门树")
	@GetMapping("/treeDept/{deptId}")
	public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap modelMap) {
		modelMap.put("dept", sysDeptService.selectDeptById(deptId));
		return "admin/sys/dept/tree";
	}

	@ApiOperation("查询代理列表")
	@RequiresPermissions("system:dept:list")
	@PostMapping("/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params) {
		List<SysDept> deptList = sysDeptService.listDept(params);
		return deptList;
	}

	@ApiOperation("进入新增代理页面")
	@GetMapping("/toAdd/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap modelMap) {
		modelMap.put("dept", sysDeptService.selectByKey(parentId));
		return prefix + "/add";
	}

	@ApiOperation("保持新增代理信息")
	@RequiresPermissions("system:dept:add")
	@PostMapping("/add")
	@ResponseBody
	public Object addSave(@Validated SysDept param, ModelMap modelMap) {
		String loginName = ShiroUtils.getLoginName();
		param.setCreateBy(loginName);
		param.setUpdateBy(loginName);
		sysDeptService.insertDept(param);
		return this.setSuccessModelMap(modelMap);
	}

	@ApiOperation("进入修改代理页面")
	@GetMapping("/toEdit/{deptId}")
	public String edit(@PathVariable("deptId") Long deptId, ModelMap modelMap) {
		SysDept dept = sysDeptService.selectDeptById(deptId);
		SysDeptVo sysDeptVo = new SysDeptVo();
		BeanUtils.copyBeanProp(sysDeptVo, dept);
		if (StringUtils.isNotNull(dept) && 100L == deptId) {
			sysDeptVo.setParentName("无");
		}
		modelMap.put("dept", sysDeptVo);
		return prefix + "/edit";
	}

	@ApiOperation("保持修改代理信息")
	@RequiresPermissions("system:dept:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Object editSave(@Validated SysDept dept, ModelMap modelMap) {
		dept.setUpdateBy(ShiroUtils.getLoginName());
		sysDeptService.updateDept(dept);
		return this.setSuccessModelMap(modelMap);
	}

	@ApiOperation("删除代理信息")
	@RequiresPermissions("system:dept:remove")
	@GetMapping("/remove/{deptId}")
	@ResponseBody
	public Object remove(@PathVariable("deptId") Long deptId, ModelMap modelMap) {
		sysDeptService.deleteDeptById(deptId);
		return this.setSuccessModelMap(modelMap);
	}

}
